; (function ($) {

	var defaultData;

	var methods = {
		//获取右边数据
		getData: function () {
			var ids = [];
			$("#muls-select option").each(function () {
				ids.push($(this).val());
			});
			return ids;
		},
		getExchange: function () {
			var defaultId = [];
			var arrAdd = [];
			var arrDel = [];
			var nowId = [];
			$.each(defaultData, function (i, item) {
				defaultId.push(item.id);
			});
			$("#muls-select option").each(function () {
				var id = $(this).val();
				var obj = {};
				var index = defaultId.indexOf(id);
				if (index < 0) {
					obj.id = id;
					obj.name = $(this).text();
					arrAdd.push(obj);
				}
				nowId.push(id);
			});
			$.each(defaultId, function (i, item) {
				var id = item;
				var obj = {};
				var index = nowId.indexOf(id);
				if (index < 0) {
					obj.id = id;
					obj.name = defaultData[i].name;
					arrDel.push(obj);
				}
			});
			var result = {
				"addList": arrAdd,
				"delList": arrDel
			}
			return result;
		}
    };
    //初始化楼层下拉列表
    var initFloorView = function (obj) {
        $.each(obj, function (i, item) {
            $("#select_floor").append("<option value='" + item.id + "'>" + item.name + "</option>");
        });
    }
	//初始化DES列表
	var initDESView = function (obj) {
		$.each(obj, function (i, item) {
			$("#muls-select-des").append("<option value='" + item.id + "'>" + item.name + "</option>");
		});
	}
	//初始化DEC列表
	var initDECView = function (obj) {
		$.each(obj, function (i, item) {
			$("#muls-select-dec").append("<option value='" + item.id + "'>" + item.name + "</option>");
		});
	}
	//初始化前门
	var initFrontDoorView = function (obj) {
		//treeDOM1 = $.fn.zTree.init($('#treeDemoL'),setting, obj);
		$.each(obj, function (i, item) {
			$("#muls-front-door").append("<option value='" + item.id + "'>" + item.name + "</option>");
		});
	}
	//初始化后门
	var initBehindDoorView = function (obj) {
		//treeDOM1 = $.fn.zTree.init($('#treeDemoL'),setting, obj);
		$.each(obj, function (i, item) {
			$("#muls-behind-door").append("<option value='" + item.id + "'>" + item.name + "</option>");
		});
	}

	$.fn.zbMulSelect = function (options) {
		if (methods[options]) {
			return methods[options].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof options === 'object' || !options) {

			var $this = $(this);
			var defaults = {
                initDataF: [],
                initDataR: [],
				initDataDES: [],
                initDataDEC: [],
                initDataAll:[]
			};

			var opts = $.extend(defaults, options);
			//defaultData = opts.initDataR;
			return this.each(function () {
				initFrontDoorView(opts.initDataF);
				initBehindDoorView(opts.initDataR);
				initDESView(opts.initDataDES);
                initDECView(opts.initDataDEC);
                initFloorView(opts.initDataAll);
				//前门
				$("#addtoR").click(function () {
					doAddToF();
				});

				$("#removeL").click(function () {
					doRemoveToF();
				});

				$("#addAll").click(function(){
					$("#muls-select").empty();
					$.each(opts.initDataF, function(i,item) {
						$("#muls-select").append("<option value='"+item.id+"'>"+item.name+"</option>");
					});
				});

				$("#removeAll").click(function () {
					$("#muls-select").empty();
				});

				//后门
				$("#addtoBehind").click(function () {
					doAddtoBehind();
				});

				$("#removeBehind").click(function () {
					doRemoveBehind();
				});
				$("#addBehindAll").click(function(){
					$("#muls-select-behind").empty();
					$.each(opts.initDataR, function(i,item) {
						$("#muls-select-behind").append("<option value='"+item.id+"'>"+item.name+"</option>");
					});
				});

				$("#removeBehindAll").click(function () {
					$("#muls-select-behind").empty();
				});
			});
		} else {
			$.error('Method ' + options + ' does not exist on jQuery.MyTest');
		}
	}

	function doAddToF() {
		$("#muls-front-door option:selected").each(function (index,item) {
			var sum = $("#muls-select option").size();
			if (sum != 0) {
				if (checkIsExist(item.value)) {
					return;
				}
			}
			$("#muls-select").append("<option value='" + item.value + "'>" + item.value + "</option>");
		});
	}

	function doRemoveToF() {
		$("#muls-select option:selected").each(function () {
			$(this).remove();
		});
	}

	function doAddtoBehind() {
		$("#muls-behind-door option:selected").each(function (index,item) {
			var sum = $("#muls-select-behind option").size();
			if (sum != 0) {
				if (checkIsExistBehind(item.value)) {
					return;
				}
			}
			$("#muls-select-behind").append("<option value='" + item.value + "'>" + item.value + "</option>");
		});
	}

	function doRemoveBehind() {
		$("#muls-select-behind option:selected").each(function () {
			$(this).remove();
		});
	}

	function checkIsExist(id) {
		var f = false;
		$("#muls-select option").each(function () {
			if (id == $(this).val()) {
				f = true;
			}
		});
		return f;
	}
	function checkIsExistBehind(id) {
		var f = false;
		$("#muls-select-behind option").each(function () {
			if (id == $(this).val()) {
				f = true;
			}
		});
		return f;
	}

})(jQuery);